# Sorting and Emotion Replication Materials##



####-------------------------------------------------------------------------------------####
rm(list=ls())
require(foreign)
require(car)
####-------------------------------------------------------------------------------------####
###  Load Raw Data


#### I. Load raw data, clean`111` and combine #######

anes.2008<-read.dta("/users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/2008 Data/anes_timeseries_2008_stata12.dta",
               convert.factors=FALSE)
anes.2012<-read.dta("/users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/2012 Data/anes_timeseries_2012_dta/anes_timeseries_2012_Stata12.dta",
                    convert.factors=FALSE)
anes.2016<-read.dta("/users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/2016 Data/anes_timeseries_2016_Stata12.dta",
                    convert.factors=FALSE)
state.fips<-unlist(strsplit(as.character(anes.2008$V081201b), "[^[:digit:]]"))
data1<-data.frame(as.numeric(state.fips[state.fips!=""]))
names(data1)<-"STATE"
data2<-data.frame(anes.2012$sample_stfips)
names(data2)<-"STATE"
data3<-data.frame(anes.2016$V163001a)
names(data3)<-"STATE"


### 2008 Data
  data1$rid<-anes.2008$V080001
  data1$CD<-recode(anes.2008$V081202b, "0=NA; 99=NA")
  #data1$ideology<-recode(anes.2008$V083069, "-9=NA; -8=NA; -7=NA") #Conservative
  #data1$ideology2<-recode(anes.2008$V083069x, "-9=NA; -8=NA; -7=NA; 1=1; 3=2; 5=3; 7=NA") #Conservative
  data1$ideology3<-(recode(anes.2008$V085084, "-9=NA; -8=NA; -7=NA; -2=NA")-1)/6#Conservative
  data1$pid<-recode(anes.2008$V083098x, "0=1; 1=2; 2=3; 3=4; 4=5; 5=6; 6=7; else=NA") #Republican
  data1$pid.3<-recode(data1$pid, "1='Democrat'; 2='Democrat'; 3='Democrat'; 4='Independent'; 5='Republican'; 6='Republican'; 7='Republican'") #Lean
  data1$year<-2008
  ###Attitudes towards House Candidates###
  #data1$ideol.dem<-(recode(anes.2008$V085085a, "-9=NA; -8=NA; -7=NA; -6=NA; -5=NA; -4=NA; -2=NA; -1=NA")-1)/6#Conservative
  #data1$ideol.rep<-(recode(anes.2008$V085085b, "-9=NA; -8=NA; -7=NA; -6=NA;-5=NA; -4=NA; -2=NA; -1=NA")-1)/6#Conservative
  

  ### Religious Attitudes #####

  data1$bible<-(recode(anes.2008$V083184, "1=3; 2=2; 3=1; else=NA")-1)/2 # Literal word of God
  data1$catholic<-recode(anes.2008$V083185b, "-9=NA; -8=NA; -4=NA; -1=NA; 2=1; else=0")
  data1$protestant<-recode(anes.2008$V083185b, "-9=NA; -8=NA; -4=NA; -1=NA; 1=1; else=0")
  data1$other.religion<-recode(anes.2008$V083185b, "-9=NA; -8=NA; -4=NA; -1=NA; 3:7=1; else=0")
  data1$no.religion<-recode(anes.2008$V083185b, "-9=NA; -8=NA; -4=NA; -1=NA; 0=1; 8=1; else=0")

    #### Personality Covariates ####
  data1$extro1<-rep(NA, dim(data1)[1])
  data1$extro2<-rep(NA, dim(data1)[1])
  data1$extraversion<-rep(NA, dim(data1)[1])
  
  
  #Region
  #data1$northeast<-recode(anes.2008$V081204,"1=1; else=0")
  #data1$north.central<-recode(anes.2008$V081204,"2=1; else=0")
  #data1$south<-recode(anes.2008$V081204,"3=1; else=0")
  #data1$west<-recode(anes.2008$V081204,"4=1; else=0")
  #Race
  data1$non.white<-recode(anes.2008$V081102,"-9=NA;-4=NA; 2:7=1; else=0")
 
  #News Consumption
  data1$news<-recode(anes.2008$V083024, "-9=NA; -1=NA")/7
  
  #Demographics
  data1$female<-recode(anes.2008$V081101,"2=1; 1=0; else=NA")
  data1$age<-anes.2008$V081104
  data1$age[anes.2008$V081104<17]<-NA
  data1$income<-anes.2008$V083248
  data1$income[anes.2008$V083248<1]<-NA
  data1$education<-recode(anes.2008$V083218x, "0:5=0; 6:7=1; -8=NA; -9=NA") #College degree

  # Racial Resentment
  data1$rr1<-recode(anes.2008$V085143, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA" ) #blacks shd work way up w/o special fav
  data1$rr2<-recode(anes.2008$V085144, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA" ) #past slavery make more diff for blacks
  data1$rr3<-recode(anes.2008$V085145, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA" ) #blacks have gotten less than deserve
  data1$rr4<-recode(anes.2008$V085146, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA" ) #blacks must try harder to get ahead

  # Emotion measures
  data1$dem.fear<-recode(anes.2008$V083063a, "1=4; 2=3; 3=2; 4=1;else=NA" ) #Fear, Dems
  data1$dem.fear[anes.2008$V083063==5]<-0
  data1$dem.fear<-data1$dem.fear/4
  data1$rep.fear<-recode(anes.2008$V083067a, "1=4; 2=3; 3=2; 4=1;else=NA" ) #Fear, Reps
  data1$rep.fear[anes.2008$V083067==5]<-0
  data1$rep.fear<-data1$rep.fear/4
  data1$dem.anger<-recode(anes.2008$V083061a, "1=4; 2=3; 3=2; 4=1;else=NA" ) #Anger, Dems
  data1$dem.anger[anes.2008$V083061==5]<-0
  data1$dem.anger<-data1$dem.anger/4
  data1$rep.anger<-recode(anes.2008$V083065a, "1=4; 2=3; 3=2; 4=1;else=NA" ) #Anger, Reps
  data1$rep.anger[anes.2008$V083065==5]<-0
  data1$rep.anger<-data1$rep.anger/4
  data1$dem.hope<-recode(anes.2008$V083062a, "1=4; 2=3; 3=2; 4=1;else=NA" ) #Hope, Dems
  data1$dem.hope[anes.2008$V083062==5]<-0
  data1$dem.hope<-data1$dem.hope/4
  data1$rep.hope<-recode(anes.2008$V083066a, "1=4; 2=3; 3=2; 4=1;else=NA" ) #Hope, Reps
  data1$rep.hope[anes.2008$V083066==5]<-0
  data1$rep.hope<-data1$rep.hope/4
  data1$dem.pride<-recode(anes.2008$V083064a, "1=4; 2=3; 3=2; 4=1;else=NA" ) #Pride, Dems
  data1$dem.pride[anes.2008$V083064==5]<-0
  data1$dem.pride<-data1$dem.pride/4
  data1$rep.pride<-recode(anes.2008$V083068a, "1=4; 2=3; 3=2; 4=1;else=NA" ) #Pride, Reps
  data1$rep.pride[anes.2008$V083068==5]<-0
  data1$rep.pride<-data1$rep.pride/4
  # Candidate thermomometers
  data1$feeling.demc<-recode(anes.2008$V083037a, "-9=NA; -8=NA; -6=NA")/100
  data1$feeling.repc<-recode(anes.2008$V083037b, "-9=NA; -8=NA; -6=NA")/100

  # Party thermometers
  data1$feeling.dems<-recode(anes.2008$V083044a, "-9=NA; -8=NA; -6=NA")/100
  data1$feeling.reps<-recode(anes.2008$V083044b, "-9=NA; -8=NA; -6=NA")/100
  
  
  
  
  # Issue questions. Only use common content questions. 
  rid<-anes.2008$V080001
  year<-rep(2008, length(rid))
  
  #Defense spending. Experimentally varied
  dspending.old<-recode(anes.2008$V083112,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Increase spending
  #new
  dspending.new<-recode(anes.2008$V083115x,"1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Increase spending
  defense.spending<-cbind(dspending.old, dspending.new)
  three.dspend<-recode(defense.spending, "1:3=1; 4=2; 5:7=3")  #High score is conservative response
  three.dspend<-three.dspend[,1]
  
  # Health Insurance
  health.old<-recode(anes.2008$V083119,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Private insurance
  health.new1<-recode(anes.2008$V083122x,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Oppose senior drug coverage
  health.new2<-recode(anes.2008$V083124x,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Oppose universal health care
  health.matrix<-cbind(health.old, health.new1, health.new2)
  three.health<-recode(health.matrix, "1:3=1; 4=2; 5:7=3")
  three.health<-three.health[,1]
  
  #Job Income Guarantee Standard of Living
  income.old<-recode(anes.2008$V083128,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Each person get ahead on own
  income.new1<-recode(anes.2008$V083131x,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Immigration job 1
  income.new2<-recode(anes.2008$V083133x,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Citizenship for illegal immigrants
  income.matrix<-cbind(income.old, income.new1, income.new2)
  three.income<-recode(income.matrix, "1:3=1; 4=2; 5:7=3")
  three.income<-three.income[,1]
  
  ##Aid to blacks
  aid.blacks<-recode(anes.2008$V083137,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Blacks help themselves
  three.blacks<-recode(aid.blacks, "1:3=1; 4=2; 5:7=3")

  ##Environment versus jobs
  environment.old<-recode(anes.2008$V083154,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Business is more important
  environment.new1<-recode(anes.2008$V083157x,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Oppose emission standards
  environment.new2<-recode(anes.2008$V083161x,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Oppose high fuel standards
  environment.new3<-recode(anes.2008$V083162x,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Oppose higher gas taxes
  environment.matrix<-cbind(environment.old, environment.new1, environment.new2, environment.new3)
  three.environment<-recode(environment.matrix, "1:3=1; 4=2; 5:7=3")
  three.environment<-three.environment[,1]
  
  ##Abortion
  abortion.old<-recode(anes.2008$V085086," 1=3; 2:3=2 ; 4=1 ;  else=NA") #Traditional opposition to abortion question
  ##Abortion.new items
  abortion.health1<-recode(anes.2008$V085092x,"1:4=NA; 5=1;  6=1;  7=2;  8=2;  9=2;  10=3;  11=3;  12=3;  13=3;  else=NA") #Oppose non lethal health risks
  abortion.health2<-recode(anes.2008$V085093x,"1=1;  2=1;  3=2;  4=2;  5=2;  6=3;  7=3;  8=3;  9=3;  else=NA") #Oppose lethal health risks
  abortion.incest<-recode(anes.2008$V085094x,"1=1;  2=1;  3=2;  4=2;  5=2;  6=3;  7=3;  8=3;  9=3; else=NA") #Oppose in cases of incest
  abortion.rape<-recode(anes.2008$V085095x,"1=1;  2=1;  3=2;  4=2;  5=2;  6=3;  7=3;  8=3;  9=3;  else=NA") #Oppose in cases of rape
  abortion.defect<-recode(anes.2008$V085096x,"1=1;  2=1;  3=2;  4=2;  5=2;  6=3;  7=3;  8=3;  9=3; else=NA") #Oppose in cases of birth defect
  abortion.hardship<-recode(anes.2008$V085097x,"1=1;  2=1;  3=2;  4=2;  5=2;  6=3;  7=3;  8=3;  9=3; else=NA") #Oppose in cases of economic hardship
  abortion.gender<-recode(anes.2008$V085098x,"1=1;  2=1;  3=2;  4=2;  5=2;  6=3;  7=3;  8=3;  9=3; else=NA") #Oppose in cases of wrong gender
  ##Stack data.
  three.abortion<-abortion.old
  
  #Old
  spending.old<-recode(anes.2008$V083105,"1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Government provide fewer services
  #New
  spending.new<-recode(anes.2008$V083108x,"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Government provide fewer services
  ##Collapse items
  #spending<-ifelse(!is.na(spending.old), spending.old, spending.new)
  spending<-cbind(spending.new, spending.old)
  three.spend<-recode(spending, "1:3=1; 4=2; 5:7=3")  ## High score is conservative response
  three.spend<-three.spend[,1]
  ######Additional Policy Questions ####
  immigration<-recode(anes.2008$V085082, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA") #What should immigration levels be
  three.immigration<-recode(immigration, "1:2=1; 3=2; 4:5=3; else=NA")
  gay.adopt<-recode(anes.2008$V083213, "1=0; 2=1; else=NA") #Do you think gay or lesbian couples, in other words, homosexual 
  gay.discrimination<-recode(anes.2008$V083211x, "1=1; 2=2; 4=3; 5=4; else=NA") #FAVOR/OPPOSE LAWS TO PROTECT GAYS AGAINST JOB DISCRIMINATION


  
## Generate a measure of ideological consistency ##  
data1$cons.score<-rowMeans(cbind(three.dspend, three.health, three.income, 
                                            three.blacks, three.environment, 
                                            three.spend, three.abortion
                                            ), na.rm=T)
data1$consistency.score<-ifelse(data1$cons.score>quantile(data1$cons.score, prob=0.8, na.rm=T) | data1$cons.score<quantile(data1$cons.score, prob=0.2, na.rm=T), 1 ,0)


### 2012 Data ###
  
  
  data2$rid<-anes.2012$caseid
  data2$CD<-anes.2012$sample_district
  ##Political dispositions
  data2$ideology3<-(recode(anes.2012$libcpre_self, "-9=NA; -8=NA; -2=NA")-1)/6 #nes.2008$V083218x#Conservative; Pretest ideology
  data2$pid<-recode(anes.2012$pid_x, "-2=NA -1=NA") ##Republican
  data2$pid.3<-recode(data2$pid, "1:2='Democrat'; 3:5='Independent'; 6:7='Republican'") #Lean
  data2$year<-2012

  #### Personality Covariates ####
  data2$extro1<-recode(anes.2012$tipi_extra, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")#Extro, Enthusiastic
  data2$extro2<-recode(anes.2012$tipi_resv, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA")#Extro, Enthusiastic
  data2$extraversion<-rowMeans(cbind(data2$extro1, data2$extro2), na.rm=T)
  
  ### Religious Attitudes 
  
  data2$bible<-(recode(anes.2012$relig_wordgod, "1=3; 2=2; 3=1; else=NA")-1)/2 #literal word of God
  data2$catholic<-recode(anes.2012$relig_7cat_x, "4=1; 1:3=0; 5:8=0; else=NA")
  data2$protestant<-recode(anes.2012$relig_7cat_x, "1:3=1; 5=1; 4=0; 6:8=0; else=NA")
  data2$other.religion<-recode(anes.2012$relig_7cat_x, "7=1; 1:6=0; 8=0; else=NA")
  data2$no.religion<-recode(anes.2012$relig_7cat_x, "9=NA; -8=NA; -4=NA; -1=NA; 3:7=1; else=0")


  data2$non.white<-recode(anes.2012$dem_raceeth_x, "2:6=1; 1=0; else=NA")
  
  #News Consumption
  data2$news<-recode(anes.2012$prmedia_wktvnws, "-9=NA; -8=NA; -4=NA")/7
  
  data2$female<-recode(anes.2012$gender_respondent_x, "1=0; 2=1; else=NA")
  data2$age<-recode(anes.2012$dem_age_r_x, "-2=NA")
  
  data2$income<-recode(as.numeric(as.character(as.factor(anes.2012$inc_incgroup_pre))),
                       "-10:0=NA; 25:30=25", )
  data2$education<-recode(anes.2012$dem_edugroup_x, "1:3=0; 4:5=1; else=NA")
  

  # Racial resentment
  data2$rr1<-recode(anes.2012$resent_workway, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA" ) #blacks shd work way up w/o special fav
  data2$rr2<-recode(anes.2012$resent_slavery, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA" ) #past slavery make more diff for blacks
  data2$rr3<-recode(anes.2012$resent_deserve, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA" ) #blacks have gotten less than deserve
  data2$rr4<-recode(anes.2012$resent_try, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA" ) #blacks must try harder to get ahead
  

  # Emotion measures
  data2$dem.fear<-recode(anes.2012$candaff_afrdpcoft, "1=4; 2=3; 3=2; 4=1; 5=0; else=NA" ) #Fear, Dems
  data2$dem.fear[anes.2012$candaff_afrdpc==2]<-0
  data2$dem.fear<-data2$dem.fear/4
  data2$rep.fear<-recode(anes.2012$candaff_afrrpcoft, "1=4; 2=3; 3=2; 4=1; 5=0; else=NA" ) #Fear, Rep
  data2$rep.fear[anes.2012$candaff_afrrpc==2]<-0
  data2$rep.fear<-data2$rep.fear/4
  data2$dem.anger<-recode(anes.2012$candaff_angdpcoft, "1=4; 2=3; 3=2; 4=1; 5=0; else=NA" ) #anger, Dems
  data2$dem.anger[anes.2012$candaff_angdpc==2]<-0
  data2$dem.anger<-data2$dem.anger/4
  data2$rep.anger<-recode(anes.2012$candaff_angrpcoft, "1=4; 2=3; 3=2; 4=1; 5=0; else=NA" ) #anger, reps
  data2$rep.anger[anes.2012$candaff_angrpc==2]<-0
  data2$rep.anger<-data2$rep.anger/4
  data2$dem.hope<-recode(anes.2012$candaff_hpdpcoft, "1=4; 2=3; 3=2; 4=1; 5=0; else=NA" ) #hope, Dems
  data2$dem.hope[anes.2012$candaff_hpdpc==2]<-0
  data2$dem.hope<-data2$dem.hope/4
  data2$rep.hope<-recode(anes.2012$candaff_hprpcoft, "1=4; 2=3; 3=2; 4=1; 5=0; else=NA" ) #hope, Reps
  data2$rep.hope[anes.2012$candaff_hprpc==2]<-0
  data2$rep.hope<-data2$rep.hope/4
  data2$dem.pride<-recode(anes.2012$candaff_prddpcoft, "1=4; 2=3; 3=2; 4=1; 5=0; else=NA" ) #pride, Dems
  data2$dem.pride[anes.2012$candaff_prddpc==2]<-0
  data2$dem.pride<-data2$dem.pride/4
  data2$rep.pride<-recode(anes.2012$candaff_prdrpcoft, "1=4; 2=3; 3=2; 4=1; 5=0; else=NA" ) #pride, Dems
  data2$rep.pride[anes.2012$candaff_prdrpc==2]<-0
  data2$rep.pride<-data2$rep.pride/4
  
  # Candidate thermomometers
  data2$feeling.demc<-recode(anes.2012$ft_dpc, "-9=NA; -8=NA; -2=NA")/100
  data2$feeling.repc<-recode(anes.2012$ft_rpc, "-9=NA; -8=NA; -2=NA")/100

  #Party thermometers
  data2$feeling.dems<-recode(anes.2012$ft_dem, "-9=NA; -8=NA; -2=NA")/100
  data2$feeling.reps<-recode(anes.2012$ft_rep, "-9=NA; -8=NA; -2=NA")/100
  
  
  
  # Issue questions. Only use common content questions. 
  rid2<-anes.2012$caseid
  year2<-rep(2012, length(rid2))
  
  
  #Defense spending

  defense.spending2<-recode(anes.2012$defsppr_self, "-9=NA; -8=NA; -2=NA") # Increase government spending on defense.
  three.dspend2<-recode(defense.spending2, "1:3=1; 4=2; 5:7=3")  #High score is conservative response
  
  # Health Insurance
  insurance2<-recode(anes.2012$inspre_self, "-9=NA; -8=NA; -2=NA") #Insurance
  three.health2<-recode(insurance2, "1:3=1; 4=2; 5:7=3")
  
  #Job Income Guarantee Standard of Living
  gov.living2<-recode(as.numeric(anes.2012$guarpr_self), "-9=NA; -8=NA; -2=NA") #Govt not see to standard of living
  three.income2<-recode(gov.living2, "1:3=1; 4=2; 5:7=3")
  
  ##Aid to blacks
  aid.blacks2<-recode(anes.2012$aidblack_self, "-9=NA; -8=NA; -2=NA") #Blacks help themselves
  three.blacks2<-recode(aid.blacks2, "1:3=1; 4=2; 5:7=3")
  
  ##Environment versus jobs
  env.jobs2<-recode(anes.2012$envjob_self, "-9=NA; -8=NA; -2=NA") #Business not regulated to protect env
  three.environment2<-recode(env.jobs2, "1:3=1; 4=2; 5:7=3")
  
  ##Abortion
  three.abortion2<-recode(anes.2012$abortpre_4point, "-9=NA; -8=NA; 5=NA; 1=3; 2:3=2; 4=1") # Oppose abortion ##There is a question wording experiment in the post study

  #Government spending
  spending2<-recode(anes.2012$spsrvpr_ssself, "-9=NA; -8=NA; -2=NA; 1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1") #Guarantee jobs
  three.spend2<-recode(spending2, "1:3=1; 4=2; 5:7=3")  ## High score is conservative response

  ######Additional Policy Questions ####
  immigration2<-recode(anes.2012$immigpo_level, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA") #What should immigration levels be
  three.immigration2<-recode(immigration2, "1:2=1; 3=2; 4:5=3; else=NA")
  gay.adopt2<-recode(anes.2012$gayrt_adopt, "1=0; 2=1; else=NA") #Do you think gay or lesbian couples, in other words, homosexual 
  gay.discrimination2<-recode(anes.2012$gayrt_discstd_x, "1=1; 2=2; 4=3; 5=4; else=NA") #FAVOR/OPPOSE LAWS TO PROTECT GAYS AGAINST JOB DISCRIMINATION
  
  
  data2$cons.score<-abs(rowMeans(cbind(three.dspend2, three.health2, three.income2, 
                                         three.blacks2, three.environment2, 
                                         three.spend2, three.abortion2), na.rm=T)-2)
  data2$consistency.score<-ifelse(data2$cons.score>quantile(data2$cons.score, prob=0.8, na.rm=T) | data2$cons.score<quantile(data2$cons.score, prob=0.2, na.rm=T), 1 ,0)
  
  ### 2016 Data
  
  data3$rid<-anes.2016$V160001
  data3$CD<-anes.2016$V163002
  data3$ideology3<-(recode(anes.2016$V161126, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")-1)/6  ### Ideology 0 1
  data3$pid<-recode(anes.2016$V161158x, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")
  data3$pid.3<-recode(data3$pid, "1='Democrat'; 2='Democrat'; 3='Democrat'; 4='Independent'; 5='Republican'; 6='Republican'; 7='Republican'")
  data3$year<-2016
  ### Exclude house candidate ideology score
  
  #### Personality Covariates ####
  data3$extro1<-recode(anes.2016$V162333, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")#Extro, Enthusiastic
  data3$extro2<-recode(anes.2016$V162338, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA")#Reserved
  data3$extraversion<-rowMeans(cbind(data3$extro1, data3$extro2), na.rm=T)
  
  
  ### Religious Attitudes #####
  data3$bible<-recode(anes.2016$V161243, "1=0; 2=0; 3=1; else=NA")  ##bible is literal word of God
  data3$catholic<-ifelse(anes.2016$V161247a==-1, recode(anes.2016$V161247b, "2=1; 1=0; 3:4=0; else=NA"), 
                         recode(anes.2016$V161247a, "2=1; 1=0; 3:4=0; else=NA"))
  data3$protestant<-ifelse(anes.2016$V161247a==-1, recode(anes.2016$V161247b, "1=1; 2:4=0; else=NA"), 
                           recode(anes.2016$V161247a, "1=1; 2:4=0; else=NA"))
  #data3$jewish<-ifelse(anes.2016$V161247a==-1, recode(anes.2016$V161247b, "3=1; 1:2=0; 4=0; else=NA"), 
  #                     recode(anes.2016$V161247a, "3=1; 1:2=0; 4=0; else=NA"))
  data3$other.religion<-ifelse(anes.2016$V161247a==-1, recode(anes.2016$V161247b, "4=1; 1:3=0; else=NA"), 
                               recode(anes.2016$V161247a, "4=1; 1:3=0; else=NA"))
  data3$no.religion<-recode(anes.2016$V161244, "2=1; 1=0; else=NA")  ### Check compatibility fo this item
  #### No Region item
  data3$non.white<-recode(anes.2016$V161310x, "1=0; 2:6=1; else=NA")
  ### News Consumption ####
  data3$news<-recode(anes.2016$V161008, "-9=NA; -8=NA")/7
  data3$female<-recode(anes.2016$V161342, "1=0; 2=1; else=NA")
  data3$age<-recode(anes.2016$V161267, "-9=NA; -8=NA")
  data3$income<-recode(anes.2016$V161361x, "-9=NA; -5=NA; 25:30=25")
  data3$education<-recode(anes.2016$V161270, "13:16=1; 1:12=0; 90=0; 95=0; else=NA")  ## College degree
  ## Racial Resentment ###
  data3$rr1<-recode(anes.2016$V162211, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") ### Blacks work hard (Irish)
  data3$rr2<-recode(anes.2016$V162212, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA") ### Generations of slavery
  data3$rr3<-recode(anes.2016$V162213, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA") ### Blacks gotten less
  data3$rr4<-recode(anes.2016$V162214, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") ### Blacks try harder
  
  ## Delete sorting measure
  # Emotion measures
  data3$dem.fear<-recode(anes.2016$V161118, "-9=NA; -8=NA" )-1 #Fear, Dems
  data3$dem.fear<-data3$dem.fear/4
  data3$rep.fear<-recode(anes.2016$V161123, "-9=NA; -8=NA" )-1 #Fear, Reps
  data3$rep.fear<-data3$rep.fear/4
  data3$dem.anger<-recode(anes.2016$V161116, "-9=NA; -8=NA" )-1 #Anger, Dems
  data3$dem.anger<-data3$dem.anger/4
  data3$rep.anger<-recode(anes.2016$V161121, "-9=NA; -8=NA" )-1 #Anger, Reps
  data3$rep.anger<-data3$rep.anger/4
  data3$dem.hope<-recode(anes.2016$V161117, "-9=NA; -8=NA" )-1 #Hope, Dems
  data3$dem.hope<-data3$dem.hope/4
  data3$rep.hope<-recode(anes.2016$V161122, "-9=NA; -8=NA" )-1 #Hope, Reps
  data3$rep.hope<-data3$rep.hope/4
  data3$dem.pride<-recode(anes.2016$V161119, "-9=NA; -8=NA" )-1 #Pride, Dems
  data3$dem.pride<-data3$dem.pride/4
  data3$rep.pride<-recode(anes.2016$V161124, "-9=NA; -8=NA" )-1 #Pride, Reps
  data3$rep.pride<-data3$rep.pride/4
  
  # Candidate thermomometers
  data3$feeling.demc<-recode(anes.2016$V161086, "-98=NA; -99=NA; -88=NA; -89=NA")/100
  data3$feeling.repc<-recode(anes.2016$V161087, "-98=NA; -99=NA; -88=NA; -89=NA")/100

  # Party thermomometers
  data3$feeling.dems<-recode(anes.2016$V161095, "-98=NA; -99=NA; -88=NA; -89=NA")/100
  data3$feeling.reps<-recode(anes.2016$V161096, "-98=NA; -99=NA; -88=NA; -89=NA")/100
  

  # Issue questions. Only use common content questions. 
  rid3<-anes.2016$V160001
  year3<-rep(2016, length(rid3))
  
  #Defense spending. 
  defense.spending3<-recode(anes.2016$V161181, "0=NA; 99=NA; -9=NA; -8=NA") #government should provide more defense spending
  three.dspend3<-recode(defense.spending3, "1:3=1; 4=2; 5:7=3")  #High score is conservative response
  #Health Insurance
  insurance3<-recode(anes.2016$V161184, "-9=NA; -8=NA; 99=NA") #Private insurance plan
  three.health3<-recode(insurance3, "1:3=1; 4=2; 5:7=3")  #High score is conservative response
  # Jobs standard of lving
  income3<-recode(anes.2016$V161189, "-9=NA; -8=NA; 99=NA") #Government should see to jobs and standard of living, conservative direction
  three.income3<-recode(insurance3, "1:3=1; 4=2; 5:7=3")  #High score is conservative response
  # Aid to blacks
  aid.blacks3<-recode(anes.2016$V161198, "-9=NA; -8=NA; 99=NA") #government should not help blacks
  three.blacks3<-recode(aid.blacks3, "1:3=1; 4=2; 5:7=3")  #High score is conservative response
  # Environment v jobs
  environment3<-recode(anes.2016$V161201, "-9=NA; -8=NA; 99=NA") #government should not help blacks
  three.environment3<-recode(environment3, "1:3=1; 4=2; 5:7=3")  #High score is conservative response
  # Abortion 
  three.abortion3<-recode(anes.2016$V161232, "1=3; 2:3=2; 4=1; -9=NA; -8=NA; 5=NA") #Ban abortion
  # Spending
  spending3<-recode(anes.2016$V161178, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; 0=NA; 99=NA; -9=NA; -8=NA") #government should provide fewer services  
  three.spend3<-recode(spending3, "1:3=1; 4=2; 5:7=3")  #High score is conservative response
  ######Additional Policy Questions ####
  immigration3<-recode(anes.2016$V162157, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA") #What should immigration levels be
  three.immigration3<-recode(immigration3, "1:2=1; 3=2; 4:5=3; else=NA")
  gay.adopt3<-recode(anes.2016$V161230, "1=0; 2=1; else=NA") #Do you think gay or lesbian couples, in other words, homosexual 
  gay.discrimination3<-recode(anes.2016$V161229x, "1=1; 2=2; 4=3; 5=4; else=NA") #FAVOR/OPPOSE LAWS TO PROTECT GAYS AGAINST JOB DISCRIMINATION
  
  data3$cons.score<-abs(rowMeans(cbind(three.dspend3, three.health3, three.income3, 
                                              three.blacks3, three.environment3, 
                                              three.spend3, three.abortion3), na.rm=T)-2)
  data3$consistency.score<-ifelse(data3$cons.score>quantile(data3$cons.score, prob=0.8, na.rm=T) | data3$cons.score<quantile(data3$cons.score, prob=0.2, na.rm=T), 1 ,0)
  

#### II. Save two datasets: Issues, for IRT and main data for analysis #######
  issues<-data.frame(
    rid=c(rid, rid2, rid3), year=c(year, year2, year3),
    black.spending=c(three.blacks, three.blacks2, three.blacks3), environment=c(three.environment, three.environment2, three.environment3),
    defense.spending=c(three.dspend, three.dspend2, three.dspend3), insurance=c(three.health, three.health2, three.health3),
    jobs=c(three.income, three.income2, three.income3), spending=c(three.spend, three.spend2, three.spend3),
    abortion=c(three.abortion, three.abortion2, three.abortion3),
    immigration=c(three.immigration, three.immigration2, three.immigration3),
    gay.discrimination=c(gay.discrimination, gay.discrimination2, gay.discrimination3),
    gay.adoption=c(gay.adopt, gay.adopt2, gay.adopt3)
  )

summary(issues)

d1<-data1[ ,order(names(data1))]
d2<-data2[ ,order(names(data2))]
d3<-data3[ ,order(names(data3))]

data<-rbind(d1,d2, d3)
data<-subset(data, !is.na(pid.3))  ### Ignore the non-respondents for pid

summary(data)


data$anger.out<-NA
data$anger.out[data$pid.3=="Democrat"]<-data$rep.anger[data$pid.3=="Democrat"]
data$anger.out[data$pid.3=="Republican"]<-data$dem.anger[data$pid.3=="Republican"]
data$anger.out[data$pid.3=="Independent"]<-rowMeans(cbind(data$dem.anger[data$pid.3=="Independent"],data$rep.anger[data$pid.3=="Independent"]), na.rm=T)

data$fear.out<-NA
data$fear.out[data$pid.3=="Democrat"]<-data$rep.fear[data$pid.3=="Democrat"]
data$fear.out[data$pid.3=="Republican"]<-data$dem.fear[data$pid.3=="Republican"]
data$fear.out[data$pid.3=="Independent"]<-rowMeans(cbind(data$dem.fear[data$pid.3=="Independent"],data$rep.fear[data$pid.3=="Independent"]), na.rm=T)

data$hope.in<-NA
data$hope.in[data$pid.3=="Democrat"]<-data$dem.hope[data$pid.3=="Democrat"]
data$hope.in[data$pid.3=="Republican"]<-data$rep.hope[data$pid.3=="Republican"]
data$hope.in[data$pid.3=="Independent"]<-rowMeans(cbind(data$dem.hope[data$pid.3=="Independent"],data$rep.hope[data$pid.3=="Independent"]), na.rm=T)


data$pride.in<-NA
data$pride.in[data$pid.3=="Democrat"]<-data$dem.pride[data$pid.3=="Democrat"]
data$pride.in[data$pid.3=="Republican"]<-data$rep.pride[data$pid.3=="Republican"]
data$pride.in[data$pid.3=="Independent"]<-rowMeans(cbind(data$dem.pride[data$pid.3=="Independent"],data$rep.pride[data$pid.3=="Independent"]), na.rm=T)

data$anger.in<-NA
data$anger.in[data$pid.3=="Democrat"]<-data$dem.anger[data$pid.3=="Democrat"]
data$anger.in[data$pid.3=="Republican"]<-data$rep.anger[data$pid.3=="Republican"]
data$anger.in[data$pid.3=="Independent"]<-rowMeans(cbind(data$dem.anger[data$pid.3=="Independent"],data$rep.anger[data$pid.3=="Independent"]), na.rm=T)


data$fear.in<-NA
data$fear.in[data$pid.3=="Democrat"]<-data$dem.fear[data$pid.3=="Democrat"]
data$fear.in[data$pid.3=="Republican"]<-data$rep.fear[data$pid.3=="Republican"]
data$fear.in[data$pid.3=="Independent"]<-rowMeans(cbind(data$dem.fear[data$pid.3=="Independent"],data$rep.fear[data$pid.3=="Independent"]), na.rm=T)

data$hope.out<-NA
data$hope.out[data$pid.3=="Democrat"]<-data$rep.hope[data$pid.3=="Democrat"]
data$hope.out[data$pid.3=="Republican"]<-data$dem.hope[data$pid.3=="Republican"]
data$hope.out[data$pid.3=="Independent"]<-rowMeans(cbind(data$dem.hope[data$pid.3=="Independent"],data$rep.hope[data$pid.3=="Independent"]), na.rm=T)


data$pride.out<-NA
data$pride.out[data$pid.3=="Democrat"]<-data$rep.pride[data$pid.3=="Democrat"]
data$pride.out[data$pid.3=="Republican"]<-data$dem.pride[data$pid.3=="Republican"]
data$pride.out[data$pid.3=="Independent"]<-rowMeans(cbind(data$dem.pride[data$pid.3=="Independent"],data$rep.pride[data$pid.3=="Independent"]), na.rm=T)

data$anxiety.in<-rowMeans(cbind(data$fear.in, data$anger.in), na.rm=T)
data$anxiety.out<-rowMeans(cbind(data$fear.out, data$anger.out), na.rm=T)
data$enthusiasm.out<-rowMeans(cbind(data$pride.out, data$hope.out), na.rm=T)
data$enthusiasm.in<-rowMeans(cbind(data$pride.in, data$hope.in), na.rm=T)

issues<-merge(issues, subset(data, select=c(rid, year, enthusiasm.in, enthusiasm.out, anxiety.in, anxiety.out)) , by=c("rid", "year"), all.x=TRUE)
save(issues, file= "/users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/issues.FINAL.Rdata")

#### IRT Model: Uncomment to Run ####

#load("/users/chrisweber/Dropbox/Project Folder/projects/Nick Davis_Polarization Emotion/data/issues.Rdata")
# require(rstan)
# rstan_options(auto_write = TRUE)
# options(mc.cores = parallel::detectCores())
# 
# ################################  Non Varying Model  #################################
# 
# y<-data.frame(issues[,3:9])
# table(is.na(rowSums(y, na.rm=T)))  ## Missing rows? No
# 
# data.base<-function(y){
#   J<-length(y[,1]) #Respondents. 
#   K<-length(y[1,]) #Items
#   N<-J*K
#   ###Create indicator for each dimension
#   jj<-rep(c(1:J), times=K) #Index for persons
#   kk<-rep(c(1:K), each=J) #Index for items
#   y<-c(as.matrix(y))
#   missing<-is.na(y)
#   miss <- which(is.na(y))
#   N <- N - length(miss)
#   jj <- jj[-miss]
#   kk <- kk[-miss]
#   y.final <- y[-miss] 
#   return(list(y.final, jj, kk, J, N, K))
# }
# a<-data.base(y)
# 
# ##Name List Elements to create final list
# list.namer<-function(l, b){
#   names(l)[[1]]<-paste0("y",b)
#   names(l)[[2]]<-paste0("jj",b)
#   names(l)[[3]]<-paste0("kk",b)
#   names(l)[[4]]<-paste0("J",b)
#   names(l)[[5]]<-paste0("N",b)
#   names(l)[[6]]<-paste0("K",b)
#   return(l)
# }
# #D=2, zero=c(0,0)
# final.list<-list()
# final.list<-append(final.list, list.namer(a, "") )
# 
# grm<-"
# data {
#   int<lower=1> J;                // number of respondents
#   int<lower=1> K;                // number of questions
#   int<lower=1> N;                // number of respondents x questions
#   int<lower=1,upper=J> jj[N];    // respondent indicator
#   int<lower=1,upper=K> kk[N];    // item indicator
#   int<lower=0,upper=10> y[N];     // response
# }
# parameters {    
#   real<lower=0>  alpha[K]; //First Dimension
#   ordered[2] beta[K];  // vector of thresholds. Three categories
#   real theta[J];      //latent variable
# }
# 
# model {
# for(k in 1:K){
#   beta[k]~cauchy(0,5);
#   alpha[k]~cauchy(0,2.5);
# }
#   theta~normal(0, 1);
# 
# for (i in 1:N) {
#   y[i] ~ ordered_logistic(theta[jj[i]]*alpha[kk[i]], beta[kk[i]]); 
#  }
# }
# "
# require(rstan)
# require(shinystan)
# fit <- stan(model_code =grm, 
#             data = final.list, iter=5000,
#             chains=1)
# #save(fit, file="/users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/latentFINAL.Rdata")
launch_shinystan(fit)  ##Reasonable convergence statistics
# 
load("/users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/latentFINAL.Rdata")
require(rstan)
d<-extract(fit, permuted=TRUE)
issues$policy.conservatism<-colMeans(d$theta)  #There is reflective invariance. Need to check the correlation if rerun the latent model

 # 

 conservatism<-subset(issues, select=c("policy.conservatism", "rid"))
 data<-merge(conservatism, data, "rid", all.y=T)
 cor.test(data$policy.conservatism, data$ideology3)
 save(data, file= "/users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/data.full.FINAL.Rdata")
# 
# 
#### III. Panel Data #######

#### Panel Data sets ####
####-------------------------------------------------------------------------------------####
####-------------------------------------------------------------------------------------####

###2008 ANES###
data.2008<-haven::read_dta("/Users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/2008 Panel/anes2008_2009panel.dta")
names(data.2008)
panel.2008<-data.frame(caseid=data.2008$caseid)
panel.2008$college.2008<-recode(data.2008$der05, "4:5=1; 3:3=0; else=NA")
panel.2008$white.2008<-recode(data.2008$der04, "1=1; 3:4=0; else=NA")
panel.2008$nonwhite.2008<-abs(1-panel.2008$white.2008)
panel.2008$income.2008=recode(data.2008$der06, "1:12=0; 13:19=1;else=NA") # 68th percentile in 1999, consistent with the cumulative
panel.2008$age.2008<-data.2008$der02
panel.2008$sex.2008<-recode(data.2008$der01, "1=0; 2=1; else=NA")
panel.2008$protestant.2008<-recode(data.2008$der22, "1=1;  2:5=0;  else=NA")
panel.2008$catholic.2008<-recode(data.2008$der22, "2=1;  1=0; 3:5=0; else=NA")
panel.2008$other.2008<-recode(data.2008$der22, "4=1;  1:3=0; 5=0; else=NA")
panel.2008$jewish.2008<-recode(data.2008$der22, "3=1;  1:2=0; 4:5=0; else=NA")
## Party Identification
panel.2008$pid.w1<-recode(data.2008$der08w1, "0=1; 1=2; 2=3; 3=4; 4=5; 5=6; 6=7; else=NA")
panel.2008$pid.w11<-recode(data.2008$der08w11, "0=1; 1=2; 2=3; 3=4; 4=5; 5=6; 6=7; else=NA")
panel.2008$pid.w17<-recode(data.2008$der08w17, "0=1; 1=2; 2=3; 3=4; 4=5; 5=6; 6=7; else=NA")
panel.2008$pid.w19<-recode(data.2008$der08w19, "0=1; 1=2; 2=3; 3=4; 4=5; 5=6; 6=7; else=NA")
panel.2008$pid.3.w1<-recode(panel.2008$pid.w1, "1:3=1;4=2; ;5:7=3;else=NA ") 
panel.2008$pid.3.w11<-recode(panel.2008$pid.w11, "1:3=1;4=2; ;5:7=3;else=NA ") 
panel.2008$pid.3.w17<-recode(panel.2008$pid.w17, "1:3=1;4=2; 5:7=3;else=NA ") 
panel.2008$pid.3.w19<-recode(panel.2008$pid.w19, "1:3=1;4=2; 5:7=3;else=NA ") 

panel.2008$news<-recode(data.2008$w1h1, "-7:-4=NA ")/7

## Ideology
panel.2008$ideo.w1<-recode(data.2008$der09w1, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")
panel.2008$ideo.w11<-recode(data.2008$der09w11, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")


#####Emotions ###
### Rely on three waves
panel.2008$dem.anger.w1<-recode(data.2008$w1w7, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$dem.hope.w1<-recode(data.2008$w1w8, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$dem.fear.w1<-recode(data.2008$w1w9, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$dem.proud.w1<-recode(data.2008$w1w10, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$rep.anger.w1<-recode(data.2008$w1w12, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$rep.hope.w1<-recode(data.2008$w1w13, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$rep.fear.w1<-recode(data.2008$w1w14, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$rep.proud.w1<-recode(data.2008$w1w15, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$dem.anger.w11<-recode(data.2008$w11wt47, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$dem.hope.w11<-recode(data.2008$w11wt48, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$dem.fear.w11<-recode(data.2008$w11wt49, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$dem.proud.w11<-recode(data.2008$w11wt50, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$rep.anger.w11<-recode(data.2008$w11wt52, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$rep.hope.w11<-recode(data.2008$w11wt53, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$rep.fear.w11<-recode(data.2008$w11wt54, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 
panel.2008$rep.proud.w11<-recode(data.2008$w11wt55, "1=5; 2=4; 3=3; 4=2; 5=1;else=NA" ) 

panel.2008<-subset(panel.2008, !is.na(panel.2008$pid.3.w1))  ### Ignore the non-respondents for pid
panel.2008<-subset(panel.2008, !is.na(panel.2008$pid.3.w11))  ### Ignore the non-respondents for pid

panel.2008$anger.out.w1<-NA
panel.2008$anger.out.w1[panel.2008$pid.3.w1==1]<-panel.2008$rep.anger.w1[panel.2008$pid.3.w1==1]
panel.2008$anger.out.w1[panel.2008$pid.3.w1==3]<-panel.2008$dem.anger.w1[panel.2008$pid.3.w1==3]
panel.2008$anger.out.w1[panel.2008$pid.3.w1==2]<-rowMeans(cbind(panel.2008$dem.anger.w1[panel.2008$pid.3.w1==2],panel.2008$rep.anger.w1[panel.2008$pid.3.w1==2]), na.rm=T)

panel.2008$fear.out.w1<-NA
panel.2008$fear.out.w1[panel.2008$pid.3.w1==1]<-panel.2008$rep.fear.w1[panel.2008$pid.3.w1==1]
panel.2008$fear.out.w1[panel.2008$pid.3.w1==3]<-panel.2008$dem.fear.w1[panel.2008$pid.3.w1==3]
panel.2008$fear.out.w1[panel.2008$pid.3.w1==2]<-rowMeans(cbind(panel.2008$dem.fear.w1[panel.2008$pid.3.w1==2],panel.2008$rep.fear.w1[panel.2008$pid.3.w1==2]), na.rm=T)

panel.2008$hope.out.w1<-NA
panel.2008$hope.out.w1[panel.2008$pid.3.w1==1]<-panel.2008$rep.hope.w1[panel.2008$pid.3.w1==1]
panel.2008$hope.out.w1[panel.2008$pid.3.w1==3]<-panel.2008$dem.hope.w1[panel.2008$pid.3.w1==3]
panel.2008$hope.out.w1[panel.2008$pid.3.w1==2]<-rowMeans(cbind(panel.2008$dem.hope.w1[panel.2008$pid.3.w1==2],panel.2008$rep.hope.w1[panel.2008$pid.3.w1==2]), na.rm=T)

panel.2008$proud.out.w1<-NA
panel.2008$proud.out.w1[panel.2008$pid.3.w1==1]<-panel.2008$rep.proud.w1[panel.2008$pid.3.w1==1]
panel.2008$proud.out.w1[panel.2008$pid.3.w1==3]<-panel.2008$dem.proud.w1[panel.2008$pid.3.w1==3]
panel.2008$proud.out.w1[panel.2008$pid.3.w1==2]<-rowMeans(cbind(panel.2008$dem.proud.w1[panel.2008$pid.3.w1==2],panel.2008$rep.hope.w1[panel.2008$pid.3.w1==2]), na.rm=T)


panel.2008$anger.in.w1<-NA
panel.2008$anger.in.w1[panel.2008$pid.3.w1==3]<-panel.2008$rep.anger.w1[panel.2008$pid.3.w1==3]
panel.2008$anger.in.w1[panel.2008$pid.3.w1==1]<-panel.2008$dem.anger.w1[panel.2008$pid.3.w1==1]
panel.2008$anger.in.w1[panel.2008$pid.3.w1==2]<-rowMeans(cbind(panel.2008$dem.anger.w1[panel.2008$pid.3.w1==2],panel.2008$rep.anger.w1[panel.2008$pid.3.w1==2]), na.rm=T)

panel.2008$fear.in.w1<-NA
panel.2008$fear.in.w1[panel.2008$pid.3.w1==3]<-panel.2008$rep.fear.w1[panel.2008$pid.3.w1==3]
panel.2008$fear.in.w1[panel.2008$pid.3.w1==1]<-panel.2008$dem.fear.w1[panel.2008$pid.3.w1==1]
panel.2008$fear.in.w1[panel.2008$pid.3.w1==2]<-rowMeans(cbind(panel.2008$dem.fear.w1[panel.2008$pid.3.w1==2],panel.2008$rep.fear.w1[panel.2008$pid.3.w1==2]), na.rm=T)

panel.2008$hope.in.w1<-NA
panel.2008$hope.in.w1[panel.2008$pid.3.w1==3]<-panel.2008$rep.hope.w1[panel.2008$pid.3.w1==3]
panel.2008$hope.in.w1[panel.2008$pid.3.w1==1]<-panel.2008$dem.hope.w1[panel.2008$pid.3.w1==1]
panel.2008$hope.in.w1[panel.2008$pid.3.w1==2]<-rowMeans(cbind(panel.2008$dem.hope.w1[panel.2008$pid.3.w1==2],panel.2008$rep.hope.w1[panel.2008$pid.3.w1==2]), na.rm=T)

panel.2008$proud.in.w1<-NA
panel.2008$proud.in.w1[panel.2008$pid.3.w1==3]<-panel.2008$rep.proud.w1[panel.2008$pid.3.w1==3]
panel.2008$proud.in.w1[panel.2008$pid.3.w1==1]<-panel.2008$dem.proud.w1[panel.2008$pid.3.w1==1]
panel.2008$proud.in.w1[panel.2008$pid.3.w1==2]<-rowMeans(cbind(panel.2008$dem.proud.w1[panel.2008$pid.3.w1==2],panel.2008$rep.hope.w1[panel.2008$pid.3.w1==2]), na.rm=T)


panel.2008$anger.out.w11<-NA
panel.2008$anger.out.w11[panel.2008$pid.3.w11==1]<-panel.2008$rep.anger.w11[panel.2008$pid.3.w11==1]
panel.2008$anger.out.w11[panel.2008$pid.3.w11==3]<-panel.2008$dem.anger.w11[panel.2008$pid.3.w11==3]
panel.2008$anger.out.w11[panel.2008$pid.3.w11==2]<-rowMeans(cbind(panel.2008$dem.anger.w11[panel.2008$pid.3.w11==2],panel.2008$rep.anger.w11[panel.2008$pid.3.w11==2]), na.rm=T)

panel.2008$fear.out.w11<-NA
panel.2008$fear.out.w11[panel.2008$pid.3.w11==1]<-panel.2008$rep.fear.w11[panel.2008$pid.3.w11==1]
panel.2008$fear.out.w11[panel.2008$pid.3.w11==3]<-panel.2008$dem.fear.w11[panel.2008$pid.3.w11==3]
panel.2008$fear.out.w11[panel.2008$pid.3.w11==2]<-rowMeans(cbind(panel.2008$dem.fear.w11[panel.2008$pid.3.w11==2],panel.2008$rep.fear.w11[panel.2008$pid.3.w11==2]), na.rm=T)

panel.2008$hope.out.w11<-NA
panel.2008$hope.out.w11[panel.2008$pid.3.w11==1]<-panel.2008$rep.hope.w11[panel.2008$pid.3.w11==1]
panel.2008$hope.out.w11[panel.2008$pid.3.w11==3]<-panel.2008$dem.hope.w11[panel.2008$pid.3.w11==3]
panel.2008$hope.out.w11[panel.2008$pid.3.w11==2]<-rowMeans(cbind(panel.2008$dem.hope.w11[panel.2008$pid.3.w11==2],panel.2008$rep.hope.w11[panel.2008$pid.3.w11==2]), na.rm=T)

panel.2008$proud.out.w11<-NA
panel.2008$proud.out.w11[panel.2008$pid.3.w11==1]<-panel.2008$rep.proud.w11[panel.2008$pid.3.w11==1]
panel.2008$proud.out.w11[panel.2008$pid.3.w11==3]<-panel.2008$dem.proud.w11[panel.2008$pid.3.w11==3]
panel.2008$proud.out.w11[panel.2008$pid.3.w11==2]<-rowMeans(cbind(panel.2008$dem.proud.w11[panel.2008$pid.3.w11==2],panel.2008$rep.hope.w11[panel.2008$pid.3.w11==2]), na.rm=T)


panel.2008$anger.in.w11<-NA
panel.2008$anger.in.w11[panel.2008$pid.3.w11==3]<-panel.2008$rep.anger.w11[panel.2008$pid.3.w11==3]
panel.2008$anger.in.w11[panel.2008$pid.3.w11==1]<-panel.2008$dem.anger.w11[panel.2008$pid.3.w11==1]
panel.2008$anger.in.w11[panel.2008$pid.3.w11==2]<-rowMeans(cbind(panel.2008$dem.anger.w11[panel.2008$pid.3.w11==2],panel.2008$rep.anger.w11[panel.2008$pid.3.w11==2]), na.rm=T)

panel.2008$fear.in.w11<-NA
panel.2008$fear.in.w11[panel.2008$pid.3.w11==3]<-panel.2008$rep.fear.w11[panel.2008$pid.3.w11==3]
panel.2008$fear.in.w11[panel.2008$pid.3.w11==1]<-panel.2008$dem.fear.w11[panel.2008$pid.3.w11==1]
panel.2008$fear.in.w11[panel.2008$pid.3.w11==2]<-rowMeans(cbind(panel.2008$dem.fear.w11[panel.2008$pid.3.w11==2],panel.2008$rep.fear.w11[panel.2008$pid.3.w11==2]), na.rm=T)

panel.2008$hope.in.w11<-NA
panel.2008$hope.in.w11[panel.2008$pid.3.w11==3]<-panel.2008$rep.hope.w11[panel.2008$pid.3.w11==3]
panel.2008$hope.in.w11[panel.2008$pid.3.w11==1]<-panel.2008$dem.hope.w11[panel.2008$pid.3.w11==1]
panel.2008$hope.in.w11[panel.2008$pid.3.w11==2]<-rowMeans(cbind(panel.2008$dem.hope.w11[panel.2008$pid.3.w11==2],panel.2008$rep.hope.w11[panel.2008$pid.3.w11==2]), na.rm=T)

panel.2008$proud.in.w11<-NA
panel.2008$proud.in.w11[panel.2008$pid.3.w11==3]<-panel.2008$rep.proud.w11[panel.2008$pid.3.w11==3]
panel.2008$proud.in.w11[panel.2008$pid.3.w11==1]<-panel.2008$dem.proud.w11[panel.2008$pid.3.w11==1]
panel.2008$proud.in.w11[panel.2008$pid.3.w11==2]<-rowMeans(cbind(panel.2008$dem.proud.w11[panel.2008$pid.3.w11==2],panel.2008$rep.hope.w11[panel.2008$pid.3.w11==2]), na.rm=T)


for (i in 1:dim(panel.2008)[2]){
  print(table(panel.2008[,i]))
}


save(panel.2008, file="/Users/chrisweber/Dropbox/Working Projects/Nick Davis_Polarization Emotion/data/panel2008FINAL.RData")


